<?php

class CSVWriter
{

	private $filename;
	private $delimiter;
	private $fields;
	private $filestream;
	
	public function __construct($config)
	{
		$this->filename = $config["output_file"];
		$this->delimiter = $config["delimiter"];
		$this->fields = $config["fields"];
		$this->filestream = fopen($this->filename, "w");
	}
	
	public function write_record($record)
	{
		$record_output = array();
		
		foreach ($this->fields as $i => $field)
		{
			$record_output[] = isset($record->$field)?$record->$field:"";
		}
		
		return fwrite($this->filestream, implode($this->delimiter, $record_output)."\n");
	}

	public function close()
	{
		fclose($this->filestream);
	}
}